<?php
require('qs_connection.php');
require('qs_functions.php');
$row = "";
@session_start();
$err_string = "";
//Check security login 
$pagesecure_level = 7;
if (strtoupper(qsrequest("logout"))=="Y") {
    $_SESSION["UserLevel"] = 0;
    $_SESSION["Logon"] = "FALSE";
    $_SESSION["UserLogon"] = "";
    $_SESSION["RedirectURL"] = qssession("firstredirecturl");
    header ("Location: ./global_group_login.php");
    exit();
}
if (qssession("Logon") != "TRUE") {
    $_SESSION["RedirectURL"] = "./" . rawurlencode("crop_source_data__edit").".php?" . qsservervars("QUERY_STRING");
    header ("Location: ./global_group_login.php");
    exit();
}
if (qssession("UserLevel") == "") {
    $_SESSION["UserLevel"] = 0;
}
if (qssession("UserLevel") < $pagesecure_level) {
    $_SESSION["Logon"] = "FALSE";
    $_SESSION["RedirectURL"] = "./" . rawurlencode("crop_source_data__edit").".php?" . qsservervars("QUERY_STRING");
    include_once('global_group_login.php');
    exit();
}

$quotechar = "`";
$quotedate = "'";
$sql = "";
$sql_ext = "";
$fields = array();
$fields[0] = "tblincomesourcescrops.ProjectID";
$fields[1] = "tblincomesourcescrops.IncomeSourceIDCrops";
$fields[2] = "tblincomesourcescrops.Unit";
$fields[3] = "tblincomesourcescrops.KcalsUnit";
$fields[4] = "tblincomesourcescrops.PriceUnit";
$fields[5] = "tblincomesourcescrops.NewPrice";
$fields[6] = "tblincomesourcescrops.ModPercent";
$formatdate = array();
$formatdate[0] = "YYYY-MM-DD HH:nn:ss";
$formatdate[1] = "";
$formatdate[2] = "";
$formatdate[3] = "";
$formatdate[4] = "";
$formatdate[5] = "";
$formatdate[6] = "";
$seperatedate = array();
$seperatedate[0] = "-";
$seperatedate[1] = " ";
$seperatedate[2] = " ";
$seperatedate[3] = " ";
$seperatedate[4] = " ";
$seperatedate[5] = " ";
$seperatedate[6] = " ";
$fields = array();
$fields[0] = "tblincomesourcescrops.ProjectID";
$fields[1] = "tblincomesourcescrops.IncomeSourceIDCrops";
$fields[2] = "tblincomesourcescrops.Unit";
$fields[3] = "tblincomesourcescrops.KcalsUnit";
$fields[4] = "tblincomesourcescrops.PriceUnit";
$fields[5] = "tblincomesourcescrops.NewPrice";
$fields[6] = "tblincomesourcescrops.ModPercent";

$fieldcons = array();
$fieldcons[0] = "tblincomesourcescrops.ProjectID";
$fieldcons[1] = "tblincomesourcescrops.IncomeSourceIDCrops";
$fieldcons[2] = "tblincomesourcescrops.Unit";
$fieldcons[3] = "tblincomesourcescrops.KcalsUnit";
$fieldcons[4] = "tblincomesourcescrops.PriceUnit";
$fieldcons[5] = "tblincomesourcescrops.NewPrice";
$fieldcons[6] = "tblincomesourcescrops.ModPercent";

$isEditables = array();
$isEditables[0] = 1;
$isEditables[1] = 1;
$isEditables[2] = 1;
$isEditables[3] = 1;
$isEditables[4] = 1;
$isEditables[5] = 1;
$isEditables[6] = 1;

$sql .= " Select\n";
    $sql .= "     tblincomesourcescrops.`ProjectID`,\n";
    $sql .= "     tblincomesourcescrops.`IncomeSourceIDCrops`,\n";
    $sql .= "     tblincomesourcescrops.`Unit`,\n";
    $sql .= "     tblincomesourcescrops.`KcalsUnit`,\n";
    $sql .= "     tblincomesourcescrops.`PriceUnit`,\n";
    $sql .= "     tblincomesourcescrops.`NewPrice`,\n";
    $sql .= "     tblincomesourcescrops.`ModPercent`\n";
    $sql .= " From\n";
    $sql .= "     tblincomesourcescrops   tblincomesourcescrops\n";

if (strpos(strtoupper($sql), " WHERE ")) {
   $sqltemp = $sql . " AND (1=0) ";
}else{
   $sqltemp = $sql . " Where (1=0) ";
}
$result = mysql_query($sqltemp . " " . $sql_ext . " limit 0,1")
          or die("Invalid query");
$qry_string = "";
$value_sql = "";
$currentrow_sql = "";
$hidden_tag = "";
$hiddenrow_tag = "";
$i = 0;
while ($i < mysql_num_fields($result)) {
    $meta = mysql_fetch_field($result);
    $field_name = $meta->name;
    $field_type = $meta->type;
    if (qsvalidRequest("search_fd" .$i)) {
        if ($qry_string == "") {
            $qry_string = "search_fd" . $i . "=" . qsrequest("search_fd" . $i);
        } else {
            $qry_string .= "&search_fd" .$i . "=" . qsrequest("search_fd" . $i);
        }
        $hidden_tag .= "<input type=\"hidden\" name=\"search_fd" . $i . "\" value=\"" . qsrequest("search_fd" . $i) . "\">\n";
        if ($qry_string == "") {
            $qry_string = "multisearch_fd" . $i . "=" . urlencode(stripslashes(qsrequest("multisearch_fd" . $i)));
        } else {
            $qry_string .= "&multisearch_fd" .$i . "=" . urlencode(stripslashes(qsrequest("multisearch_fd" . $i)));
        }
        $hidden_tag .= "<input type=\"hidden\" name=\"multisearch_fd" .$i . "\" value=\"" . qsreplace_html_quote(stripslashes(qsrequest("multisearch_fd" . $i))) . "\">\n";
    }
    $type_field = "";
    $type_field = returntype($field_type);
    $quotedata = "";
    switch ($type_field) {
      case "type_datetime": $quotedata = $quotedate; break;
      case "type_string": $quotedata = "'"; break;
      case "type_integer": $quotedata = ""; break;
      case "type_unknown": $quotedata = "'"; break;
      default: $quotedata = "'";
    } 
    if ($meta) {
        if (qsrequest("currentrow_fd" .$i) != "") {
            if ($currentrow_sql == "") {
                $currentrow_sql  = $fields[$i] . " = " . $quotedata . ereg_replace("'","''",stripslashes(qsrequest("currentrow_fd" . $i))) . $quotedata;
            } else {
                $currentrow_sql .= " and " .$fields[$i] . " = " . $quotedata . ereg_replace("'","''",stripslashes(qsrequest("currentrow_fd" . $i))) . $quotedata;
            }
            $hiddenrow_tag .= "<input type=\"hidden\" name=\"currentrow_fd" . $i . "\" value=\"" . qsreplace_html_quote(stripslashes(qsrequest("currentrow_fd" . $i))) . "\">\n";
        }
      if($isEditables[$i])
      {
        if ($type_field == "type_datetime") {
            if (qsvalidRequest("edit_fd" . $i)) {
                $idata = qsrequest("edit_fd" . $i);
                $value_sql .= "," . $quotechar . $field_name . $quotechar . " = " . $quotedate . qsconvertdate2ansi($idata,$formatdate[$i],$seperatedate[$i]) . $quotedate;
            }
            else if($isEditables[$i] != 2) {
		             $value_sql .= "," . $quotechar . $field_name . $quotechar . " = null"; 
	           }
        } elseif ($type_field == "type_integer") {
            if (qsvalidRequest("edit_fd" . $i)) {
                $idata = qsrequest("edit_fd" . $i);
                if (is_numeric($idata)) {
                        $value_sql  .= "," . $quotechar . $field_name . $quotechar . " = " . $idata;
                } else {
                    $err_string .= "<strong>Error:</strong>while updating<strong>" . $field_name . "</strong>.<br>";
                    $err_string .= "Description: Type mismatch.<br>";
                }
            }
			       else if($isEditables[$i] != 2) {
		             $value_sql .= "," . $quotechar . $field_name . $quotechar . " = null"; 
	           }
        } elseif ($type_field == "type_string") {
            if (qsvalidRequest("edit_fd" . $i)) {
                $idata = qsrequest("edit_fd" . $i);
                $value_sql  .= "," . $quotechar . $field_name . $quotechar . " = '" . ereg_replace("'","''",stripslashes($idata)) . "'";
            }
			       else if($isEditables[$i] != 2) {
		             $value_sql .= "," . $quotechar . $field_name . $quotechar . " = null"; 
	           }
        } else {
            if (qsvalidRequest("edit_fd" . $i)) {
                $idata = qsrequest("edit_fd" . $i);
                $value_sql  .= "," . $quotechar . $field_name . $quotechar . " = '" . ereg_replace("'","''",stripslashes($idata)) . "'";
            }
			       else if($isEditables[$i] != 2) {
		             $value_sql .= "," . $quotechar . $field_name . $quotechar . " = null"; 
	           }
        }
      }//if ($isEditables[$i])
    }//if ($meta)
$i++;
}
if (isset($_POST["act"])) {
if (($err_string)=="") {
    if (($value_sql)!="") {
        if (substr($value_sql, 0, 1) == ",") {
           $value_sql = substr($value_sql, 1);
        }
        $sql  = "";
        $sql  = "update " . $quotechar . mysql_field_table($result,0) . $quotechar;
        $sql .= " set " . $value_sql;
        $sql .= " where ";
        $sql .= $currentrow_sql;
      if ($result > 0) {mysql_free_result($result);}
      if (!$result = @mysql_query($sql)){
        $err_string .= "<strong>Error:</strong>while updating<br>" . mysql_error();
      }
    }
#----get submit url page----
    $submiturl = "./crop_source_data_.php?";
    if ($err_string == "") {
        if ($qry_string != "") {
            $URL= $submiturl . "&" . $qry_string;
        } else {
            $URL= $submiturl;
        }
            header ("Location: $URL");
            exit;
        }
    }
}
else
{
    if ($result > 0) {mysql_free_result($result);}
    $sql = "";
$sql .= " Select\n";
    $sql .= "     tblincomesourcescrops.`ProjectID`,\n";
    $sql .= "     tblincomesourcescrops.`IncomeSourceIDCrops`,\n";
    $sql .= "     tblincomesourcescrops.`Unit`,\n";
    $sql .= "     tblincomesourcescrops.`KcalsUnit`,\n";
    $sql .= "     tblincomesourcescrops.`PriceUnit`,\n";
    $sql .= "     tblincomesourcescrops.`NewPrice`,\n";
    $sql .= "     tblincomesourcescrops.`ModPercent`\n";
    $sql .= " From\n";
    $sql .= "     tblincomesourcescrops   tblincomesourcescrops\n";
  if ($currentrow_sql != "") {
    $sql .= " where ".$currentrow_sql ;
  }
    $result =  mysql_query($sql) or die("Invalid query");
    $row = mysql_fetch_array($result);
}
?>
<HTML>
<HEAD>
<Title>Update Crop Source Data </Title>
<link rel="stylesheet" type="text/css" href="crop_source_data__edit.css">
<script language="javascript">
function Trim(s){
    var temp = " ";
    var i = 0;
    while ((temp == " ") && (i <= s.length)) {
        temp = s.charAt(i);
        i++;
    }
    s = s.substring(i - 1, s.length);
    return(s);
}
function check(frm) {
    var szAlert = "Invalid\n";
    var nIndex = 0;
    if (!RequiredField(frm.edit_fd1.value)) {
        nIndex++;
        szAlert += "- " +"'Income Source IDCrops' cannot be blank\n";
    }
    if (!NumberValidate(frm.edit_fd1.value)) {
        nIndex++;
        szAlert += "- " +"'Income Source IDCrops' invalid numeric format\n";
    }
    if (!NumberValidate(frm.edit_fd3.value)) {
        nIndex++;
        szAlert += "- " +"'Kcals Unit' invalid numeric format\n";
    }
    if (!NumberValidate(frm.edit_fd4.value)) {
        nIndex++;
        szAlert += "- " +"'Price Unit' invalid numeric format\n";
    }
    if (!NumberValidate(frm.edit_fd5.value)) {
        nIndex++;
        szAlert += "- " +"'New Price' invalid numeric format\n";
    }
    if (!NumberValidate(frm.edit_fd6.value)) {
        nIndex++;
        szAlert += "- " +"'Mod Percent' invalid numeric format\n";
    }
    if(nIndex > 0) {
       	alert(szAlert) ;
      	return false ;
    }
    return true ;
}
</script>
<script src="validate.js"></script>

<meta http-equiv="Content-Type" content="text/html; utf-8">
<meta name="generator" content="dbQwikSite Ecommerce">
</HEAD>
<BODY><script language="javascript" src="./js/qs_menu.js"></script>
<CENTER>
<p align="left"><img style="left: 10px; top: 15px" alt="efd" hspace="0" src="http://www.evidencefordevelopment.com/templates/247extender_b/images/efd_100x65.png" align="baseline" border="0" /></p><CENTER>
<center><strong><font size="5">
Update Crop Source Data 
</font></strong></center><br>

<A NAME=top></A>

<script>
function getURLParam(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
  var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
  var aQueryString = strQueryString.split("&");
  for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
    if (
	aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
      var aParam = aQueryString[iParam].split("=");
      strReturn = aParam[1];
      break;
    }
  }
}
return strReturn;
}
</script>


<Form name="qs_edit_form" method="post" action="./crop_source_data__edit.php" onSubmit="return check(this)">
<script language='javascript' src='qwikcalendar.js'></script>
<?php
print $hidden_tag;
print $hiddenrow_tag;
?>
<Table Border="0" Cellpadding="2" Cellspacing="1" BgColor="#10783F">

<?php
$css_class = "\"TrOdd\"";
?>
<tr>
<td colspan="2" class="ThRows">Update Crop Source Data </td>
</tr>
<?php
if ($err_string != "") {
    print "<tr>";
    print "<td class=\"ThRows\"><Strong>Error:</Strong></td>";
    print "<td colspan=\"2\" class=" . $css_class . " align=Default>" . $err_string . "</td>";
    print "</tr>";
}
?>
<tr>
<?php
if ((!isset($_GET["edit_fd0"])) && (!isset($_POST["edit_fd0"]))) {
    $itemvalue = $row[0];
} else {
    $itemvalue = qsrequest("edit_fd0");
}

    $cellvalue = "<input type=\"hidden\" name=\"edit_fd0\" value=\"". qsreplace_html_quote(stripslashes($itemvalue)) ."\">";
    if ($cellvalue == "") {
        $cellvalue = "&nbsp;";
    }
    print $cellvalue;
?>
<td class="ThRows">Crops</td>
<?php
$cellvalue = "";
if ((!isset($_GET["edit_fd1"])) && (!isset($_POST["edit_fd1"]))) {
    $itemvalue = $row[1];
} else {
    $itemvalue = qsrequest("edit_fd1");
}

    $cellvalue = "<select name=\"edit_fd1\" >" . qsmysqlgen_listbox(" Select     tbllkupincomesources.`IncomeSourceID`,     tbllkupincomesources.`IncomeSource`,     tbllkupincomesources.`IncomeType` From     tbllkupincomesources   tbllkupincomesources Where       ( tbllkupincomesources.IncomeType = 'CROPS' )   ","edit_fd1","IncomeSourceID","IncomeSource",$itemvalue) . "</select>";
    if ($cellvalue == "") {
        $cellvalue = "&nbsp;";
    }
    print "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>";
?>
</tr>
<tr>
<td class="ThRows">Unit</td>
<?php
$cellvalue = "";
if ((!isset($_GET["edit_fd2"])) && (!isset($_POST["edit_fd2"]))) {
    $itemvalue = $row[2];
} else {
    $itemvalue = qsrequest("edit_fd2");
}

    $cellvalue = "<input type=\"text\" name=\"edit_fd2\" value=\"" . qsreplace_html_quote(stripslashes($itemvalue)) . "\" size=\"30\"  maxlength=\"50\" >";
    if ($cellvalue == "") {
        $cellvalue = "&nbsp;";
    }
    print "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>";
?>
</tr>
<tr>
<td class="ThRows">Kcals Unit</td>
<?php
$cellvalue = "";
if ((!isset($_GET["edit_fd3"])) && (!isset($_POST["edit_fd3"]))) {
    $itemvalue = $row[3];
} else {
    $itemvalue = qsrequest("edit_fd3");
}

    $cellvalue = "<input type=\"text\" name=\"edit_fd3\" value=\"" . qsreplace_html_quote(stripslashes($itemvalue)) . "\">";
    if ($cellvalue == "") {
        $cellvalue = "&nbsp;";
    }
    print "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>";
?>
</tr>
<tr>
<td class="ThRows">Price Unit</td>
<?php
$cellvalue = "";
if ((!isset($_GET["edit_fd4"])) && (!isset($_POST["edit_fd4"]))) {
    $itemvalue = $row[4];
} else {
    $itemvalue = qsrequest("edit_fd4");
}

    $cellvalue = "<input type=\"text\" name=\"edit_fd4\" value=\"" . qsreplace_html_quote(stripslashes($itemvalue)) . "\">";
    if ($cellvalue == "") {
        $cellvalue = "&nbsp;";
    }
    print "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>";
?>
</tr>
<tr>
<td class="ThRows">New Price</td>
<?php
$cellvalue = "";
if ((!isset($_GET["edit_fd5"])) && (!isset($_POST["edit_fd5"]))) {
    $itemvalue = $row[5];
} else {
    $itemvalue = qsrequest("edit_fd5");
}

    $cellvalue = "<input type=\"text\" name=\"edit_fd5\" value=\"" . qsreplace_html_quote(stripslashes($itemvalue)) . "\">";
    if ($cellvalue == "") {
        $cellvalue = "&nbsp;";
    }
    print "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>";
?>
</tr>
<tr>
<td class="ThRows">Mod Percent</td>
<?php
$cellvalue = "";
if ((!isset($_GET["edit_fd6"])) && (!isset($_POST["edit_fd6"]))) {
    $itemvalue = $row[6];
} else {
    $itemvalue = qsrequest("edit_fd6");
}

    $cellvalue = "<input type=\"text\" name=\"edit_fd6\" value=\"" . qsreplace_html_quote(stripslashes($itemvalue)) . "\">";
    if ($cellvalue == "") {
        $cellvalue = "&nbsp;";
    }
    print "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>";
?>
</tr>
<?php
#----get back url page----
  $backurl = "./crop_source_data_.php?";
?>
<tr>
<td class="ThRows">&nbsp;</td>
<td class="TrOdd" align=Default>
<input type="hidden" name="act" value="n">
<input type="button" name="QS_Back" value="Back" OnClick="javascript:window.location='<?php print $backurl; ?>'">&nbsp;&nbsp;
<input type="submit" name="QS_Submit" value="Update">&nbsp;&nbsp;
<input type="reset" name="QS_Reset" value="Reset">
</td>
</tr>
</Table><br>
</Form>
<?php
if ($link > 0) {mysql_close($link);}
?>
<A NAME=bottom></A>
<div align="center"><a href="./project_setup.php" target="Back to Project Setup">Back to Project Setup</a></div>
</CENTER>

</BODY>
</HTML>
